package com.roshan.web.util.interceptors;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.roshan.web.util.constants.CommonConstants;

public class PropertyHandlerInterceptorAdapter extends HandlerInterceptorAdapter {

    private Map<String, Object> propertyMap = new LinkedHashMap<String, Object>();

    @Override
    public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler,
            final ModelAndView modelAndView) throws Exception {
        if (modelAndView != null) {

            for (Map.Entry<String, Object> entry : this.propertyMap.entrySet()) {
                Object value = entry.getValue();
                if (value instanceof String
                        && (CommonConstants.TRUE.equalsIgnoreCase((String) value) || CommonConstants.FALSE
                                .equalsIgnoreCase((String) value))) {

                    value = Boolean.valueOf(((String) value).toLowerCase());

                }

                modelAndView.addObject(entry.getKey(), value);
            }

        }
        super.postHandle(request, response, handler, modelAndView);
    }

    /**
     * Gets the property map.
     * 
     * @return the propertyMap
     */
    public Map<String, Object> getPropertyMap() {
        return this.propertyMap;
    }

    /**
     * Sets the property map.
     * 
     * @param propertyMap the propertyMap to set
     */
    public void setPropertyMap(Map<String, Object> propertyMap) {
        this.propertyMap = propertyMap;
    }

}
